﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fcdbas.CodeBuilder
{
    public partial class NewLogicUnit : Form
    {
        public NewLogicUnit()
        {
            InitializeComponent();
            //this.labName.Text = "Project Name";
            //this.Text = "Project";
        }


        public NewLogicUnit(int flag)
        {
            this.Flag = flag;
            InitializeComponent();
            //this.labName.Text = this.Flag == 0 ? "Project Name" : "Model Name";
            //this.Text = this.Flag == 0 ? "Project" : "Model";
        }


        /// <summary>
        /// 0,Project
        /// 1,Model
        /// </summary>
        public int Flag
        {
            get;
            set;
        }

        #region 按鈕事件方法
        private void button1_Click(object sender, EventArgs e)
        {
            (this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit = new TemplateCode.LogicUnit();
            (this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit.TableName = this.txtName.Text;
            (this.Owner as Fcdbas.Tool.CodeBuilder).logicUnit = (this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit;
            if( (this.Owner as Fcdbas.Tool.CodeBuilder).LogicUnitLits==null)
            {
                (this.Owner as Fcdbas.Tool.CodeBuilder).LogicUnitLits = new List<TemplateCode.LogicUnit>();
            }
            (this.Owner as Fcdbas.Tool.CodeBuilder).LogicUnitLits.Add((this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit);
            //(this.Owner as Fcdbas.Tool.CodeBuilder).logicUnit.CDBFile = this.txtCDBFile.Text;
            //(this.Owner as Fcdbas.Tool.CodeBuilder).logicUnit.PKGFile = this.txtPAKFile.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "模版文件(*.cdb)|*.cdb";
            if (DialogResult.OK == this.openFileDialog1.ShowDialog())
            {
                string file = this.openFileDialog1.FileName;
                this.txtCDBFile.Text = file;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "模版文件(*.pkg)|*.pkg";
            if (DialogResult.OK == this.openFileDialog1.ShowDialog())
            {
                string file = this.openFileDialog1.FileName;
                this.txtPAKFile.Text = file;
            }
        }

        #endregion


        private void ConfirmOK()
        {
            (this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit = new TemplateCode.LogicUnit();
            (this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit.TableName = this.txtName.Text;
            (this.Owner as Fcdbas.Tool.CodeBuilder).logicUnit = (this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit;
            if ((this.Owner as Fcdbas.Tool.CodeBuilder).LogicUnitLits == null)
            {
                (this.Owner as Fcdbas.Tool.CodeBuilder).LogicUnitLits = new List<TemplateCode.LogicUnit>();
            }
            (this.Owner as Fcdbas.Tool.CodeBuilder).LogicUnitLits.Add((this.Owner as Fcdbas.Tool.CodeBuilder).CurLogiUnit);
            //(this.Owner as Fcdbas.Tool.CodeBuilder).logicUnit.CDBFile = this.txtCDBFile.Text;
            //(this.Owner as Fcdbas.Tool.CodeBuilder).logicUnit.PKGFile = this.txtPAKFile.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                ConfirmOK();
            }
        }
        
    }
}
